home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP13.ZIP / PATRON / IPERFILE.CPP < prev    next >
C/C++ Source or Header  |  1993-07-19  |  6KB  |  252 lines

  1. /*
  2.  * IPERFILE.CPP
  3.  *
  4.  * Implementation of the IPersistFile interface for Patron's documents.
  5.  *
  6.  * Copyright (c)1992 Microsoft Corporation, All Rights Reserved
  7.  *
  8.  * Kraig Brockschmidt, Software Design Engineer
  9.  * Microsoft Systems Developer Relations
  10.  *
  11.  * Internet  :  kraigb@microsoft.com
  12.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  13.  */
  14.  
  15.  
  16. #include "patron.h"
  17.  
  18.  
  19. /*
  20.  * CImpIPersistFile:CImpIPersistFile
  21.  * CImpIPersistFile::~CImpIPersistFile
  22.  *
  23.  * Constructor Parameters:
  24.  *  pDoc            LPCPatronDoc associated with this interface
  25.  *  punkOuter       LPUNKNOWN of the controlling unknown.
  26.  */
  27.  
  28. CImpIPersistFile::CImpIPersistFile(LPCPatronDoc pDoc, LPUNKNOWN punkOuter)
  29.     {
  30.     m_cRef=0;
  31.     m_pDoc=pDoc;
  32.     m_punkOuter=punkOuter;
  33.     return;
  34.     }
  35.  
  36.  
  37. CImpIPersistFile::~CImpIPersistFile(void)
  38.     {
  39.     return;
  40.     }
  41.  
  42.  
  43.  
  44.  
  45. /*
  46.  * CImpIPersistFile::QueryInterface
  47.  * CImpIPersistFile::AddRef
  48.  * CImpIPersistFile::Release
  49.  *
  50.  * Purpose:
  51.  *  Standard set of IUnknown members for this interface
  52.  */
  53.  
  54. STDMETHODIMP CImpIPersistFile::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  55.     {
  56.     return m_punkOuter->QueryInterface(riid, ppv);
  57.     }
  58.  
  59. STDMETHODIMP_(ULONG) CImpIPersistFile::AddRef(void)
  60.     {
  61.     ++m_cRef;
  62.     return m_punkOuter->AddRef();
  63.     }
  64.  
  65. STDMETHODIMP_(ULONG) CImpIPersistFile::Release(void)
  66.     {
  67.     --m_cRef;
  68.     return m_punkOuter->Release();
  69.     }
  70.  
  71.  
  72.  
  73.  
  74. /*
  75.  * CImpIPersistFile::GetClassID
  76.  *
  77.  * Purpose:
  78.  *  Returns the CLSID of the file represented by this interface.
  79.  *
  80.  * Parameters:
  81.  *  pClsID          LPCLSID in which to store our CLSID.
  82.  *
  83.  * Return Value:
  84.  *  HRESULT         Standard
  85.  */
  86.  
  87. STDMETHODIMP CImpIPersistFile::GetClassID(LPCLSID pClsID)
  88.     {
  89.     *pClsID=CLSID_PatronPages;
  90.     return NOERROR;
  91.     }
  92.  
  93.  
  94.  
  95.  
  96.  
  97. /*
  98.  * CImpIPersistFile::IsDirty
  99.  *
  100.  * Purpose:
  101.  *  Tells the caller if we have made changes to this file since
  102.  *  it was loaded or initialized new.
  103.  *
  104.  * Parameters:
  105.  *  None
  106.  *
  107.  * Return Value:
  108.  *  HRESULT         Contains S_OK if we ARE dirty, S_FALSE if NOT dirty,
  109.  *                  that is, "Yes I AM dirty, or NO, I'm clean."
  110.  */
  111.  
  112. STDMETHODIMP CImpIPersistFile::IsDirty(void)
  113.     {
  114.     return ResultFromScode(m_pDoc->FDirtyGet() ? S_OK : S_FALSE);
  115.     }
  116.  
  117.  
  118.  
  119.  
  120.  
  121. /*
  122.  * IPersistFile::Load
  123.  *
  124.  * Purpose:
  125.  *  Asks the server to load the document for the given filename.
  126.  *
  127.  * Parameters:
  128.  *  pszFile         LPCSTR to the filename to load.
  129.  *  grfMode         DWORD containing open flags requested from the caller.
  130.  *                  Currently these are safely ignored.
  131.  *
  132.  * Return Value:
  133.  *  HRESULT         Standard
  134.  */
  135.  
  136. STDMETHODIMP CImpIPersistFile::Load(LPCSTR pszFile, DWORD grfMode)
  137.     {
  138.     UINT        uRet;
  139.  
  140.     uRet=m_pDoc->ULoad(TRUE, (LPSTR)pszFile);
  141.     return (DOCERR_NONE==uRet) ? NOERROR : ResultFromScode(STG_E_READFAULT);
  142.     }
  143.  
  144.  
  145.  
  146.  
  147.  
  148. /*
  149.  * IPersistFile::Save
  150.  *
  151.  * Purpose:
  152.  *  Instructs the server to write the current file into a new filename,
  153.  *  possibly then using that filename as the current one.
  154.  *
  155.  * Parameters:
  156.  *  pszFile         LPCSTR of the file into which we save.  If NULL,
  157.  *                  this means save the current file.
  158.  *  fRemember       BOOL indicating if we're to use this filename as
  159.  *                  the current file now (Save As instead of Save Copy As).
  160.  *
  161.  * Return Value:
  162.  *  HRESULT         Standard
  163.  */
  164.  
  165. STDMETHODIMP CImpIPersistFile::Save(LPCSTR pszFile, BOOL fRemember)
  166.     {
  167.     UINT        uRet;
  168.  
  169.     /*
  170.      * Since we don't want to mess with changing USave (which would
  171.      * require changes to CLASSLIB (urk) we instead save fRemember in
  172.      * the document before calling USave which supresses the call
  173.      * to CPatronDoc::Rename if FALSE.
  174.      */
  175.  
  176.     m_pDoc->m_fRename=fRemember;
  177.     uRet=m_pDoc->USave(0, (LPSTR)pszFile);
  178.     m_pDoc->m_fRename=TRUE;
  179.  
  180.     return (DOCERR_NONE==uRet) ? NOERROR : ResultFromScode(STG_E_WRITEFAULT);
  181.     }
  182.  
  183.  
  184.  
  185.  
  186.  
  187. /*
  188.  * IPersistFile::SaveCompleted
  189.  *
  190.  * Purpose:
  191.  *  Informs us that the operation that called Save is now finished and
  192.  *  we can access the file again.
  193.  *
  194.  * Parameters:
  195.  *  pszFile         LPCSTR to the file that was saved.
  196.  *
  197.  * Return Value:
  198.  *  HRESULT         Standard
  199.  */
  200.  
  201. STDMETHODIMP CImpIPersistFile::SaveCompleted(LPCSTR pszFile)
  202.     {
  203.     return NOERROR;
  204.     }
  205.  
  206.  
  207.  
  208.  
  209. /*
  210.  * IPersistFile::GetCurFile
  211.  *
  212.  * Purpose:
  213.  *  Retrieves the name of the current file.
  214.  *
  215.  * Parameters:
  216.  *  ppszFile        LPSTR FAR * into which we store a pointer to the
  217.  *                  filename that should be allocated with the shared
  218.  *                  IMalloc.
  219.  *
  220.  * Return Value:
  221.  *  HRESULT         Standard
  222.  */
  223.  
  224. STDMETHODIMP CImpIPersistFile::GetCurFile(LPSTR FAR * ppszFile)
  225.     {
  226.     LPMALLOC    pIMalloc;
  227.     LPSTR       psz;
  228.     UINT        uRet;
  229.  
  230.     *ppszFile=NULL;
  231.  
  232.     if (FAILED(CoGetMalloc(MEMCTX_SHARED, &pIMalloc)))
  233.         return ResultFromScode(E_FAIL);
  234.  
  235.     psz=(LPSTR)pIMalloc->Alloc(OLEUI_CCHPATHMAX);
  236.     pIMalloc->Release();
  237.  
  238.     uRet=m_pDoc->FilenameGet(psz, OLEUI_CCHPATHMAX);
  239.  
  240.     //If we have no filename, return the prompt for a File Open/Save.
  241.     if (0==uRet)
  242.         {
  243.         //The load string at psz+2 puts the extension past the "*."
  244.         lstrcpy(psz, "*.");
  245.         LoadString(m_pDoc->m_hInst, IDS_DEFEXT
  246.             , (LPSTR)(psz+2), OLEUI_CCHPATHMAX-2);
  247.         }
  248.  
  249.     *ppszFile=psz;
  250.     return (0==uRet) ? ResultFromScode(S_FALSE) : NOERROR;
  251.     }
  252.